package com.retrozelda.nyanimals;

import android.graphics.RectF;
import android.util.Log;

public class CButton extends INyanObject
{
	CRectangle m_UpRect 		= new CRectangle();
	CRectangle m_PressedRect	= new CRectangle();
	boolean m_bPrevTouchState	= false; // false = up.  true = pressed
	boolean m_bTouchState		= false; // false = up.  true = pressed

	// keep these here to pass to the rects
	int[] m_nPosition = new int[2];
	int[] m_nSize = new int[2];
	
	
	public boolean GetTouchState()
	{
		return m_bTouchState;
	}
	
	public void SetTouchState(boolean bTouch)
	{
		m_bTouchState = bTouch;
		UpdateVisibility(m_bTouchState);
	}
	
	public void ToggleTouchState()
	{
		SetTouchState(!m_bTouchState);
	}
	
	@Override
	public Object clone()
	{
		CButton ret = new CButton();

		return ret;
	}
	
	public int[] GetPosition()
	{
		return m_nPosition;
	}
	public void SetPosition(int nPosX, int nPosY)
	{
		m_nPosition[0] = nPosX;
		m_nPosition[1] = nPosY;
		m_UpRect.SetPosition(nPosX, nPosY);
		m_PressedRect.SetPosition(nPosX, nPosY);
	}

	public int[] GetSize()
	{
		return m_nSize;
	}
	public void SetSize(int nSizeX, int nSizeY)
	{
		m_nSize[0] = nSizeX;
		m_nSize[1] = nSizeY;
		m_UpRect.SetSize(nSizeX, nSizeY);
		m_PressedRect.SetSize(nSizeX, nSizeY);
	}

	@Override
	public void Init(NyanimalGLSurfaceView game) 
	{
		// TODO Auto-generated method stub
		m_UpRect.Init(game);
		m_UpRect.SetDraw(false);
		m_PressedRect.Init(game);
		m_PressedRect.SetDraw(false);
		
	}
	
	public void Init(NyanimalGLSurfaceView game, int nPosX, int nPosY, int nSizeX, int nSizeY, int nUpResourceID, int nPressedResourceID)
	{
		m_nPosition[0] = nPosX;
		m_nPosition[1] = nPosY;
		m_nSize[0] = nSizeX;
		m_nSize[1] = nSizeY;

		CTexture upTexture = CTextureManager.GetInstance().LoadTexture(nUpResourceID, game);
		CTexture pressedTexture = CTextureManager.GetInstance().LoadTexture(nPressedResourceID, game);
		
		m_UpRect.Init(game);
		m_UpRect.SetTexture(upTexture);
		m_UpRect.SetPosition(nPosX, nPosY);
		m_UpRect.SetSize(nSizeX, nSizeY);
		m_UpRect.SetDraw(true);
		
		m_PressedRect.Init(game);
		m_PressedRect.SetTexture(pressedTexture);
		m_PressedRect.SetPosition(nPosX, nPosY);
		m_PressedRect.SetSize(nSizeX, nSizeY);
		m_PressedRect.SetDraw(false);

	}


	// in: touch position
	// ret: 0 = button is up.  1 = pressed.  2 = held.  3 = released
	public int CheckTouch(int touchX, int touchY)
	{
		RectF rectToCheck = null;
		if(m_UpRect.IsDrawing() == true)
		{
			rectToCheck = m_UpRect.m_BoundingRect;
		}
		else
		{
			rectToCheck = m_PressedRect.m_BoundingRect;		
		}
		
		int nReturn = 0;
		if(rectToCheck != null)
		{
			m_bPrevTouchState = m_bTouchState;
			m_bTouchState = rectToCheck.contains((float)touchX, (float)touchY);
						
			// update the showing rect
			if(m_bTouchState != m_bPrevTouchState)
			{
				UpdateVisibility(m_bTouchState);
			}			
			
			// up
			if(m_bTouchState == false && m_bPrevTouchState == false)
			{
				nReturn = 0;
			}
			
			// pressed
			if(m_bTouchState == true && m_bPrevTouchState == false)
			{
				nReturn = 1;
			}
			
			// holding
			if(m_bTouchState == true && m_bPrevTouchState == true)
			{
				nReturn = 2;
			}
			
			// released
			if(m_bTouchState == false && m_bPrevTouchState == true)
			{
				nReturn = 3;
			}

			// Log.d(CButton.class.getSimpleName(), "button touch: " + touchX + " " + touchY + " rect:" + m_Rect.toString());
		}
		else	
		{
			Log.d(CButton.class.getSimpleName(), "Invalid Button!");
		}
		if(nReturn != 0 && nReturn != 1 && nReturn != 2)
			Log.d(CButton.class.getSimpleName(), "button state: " + nReturn);
		return nReturn;
	}
	
	private void UpdateVisibility(boolean bTouch)
	{
		m_UpRect.SetDraw(!bTouch);
		m_PressedRect.SetDraw(bTouch);
	}


	@Override
	public void Update(float deltaTime) 
	{
		// TODO Auto-generated method stub
		m_UpRect.Update(deltaTime);
		m_PressedRect.Update(deltaTime);
	}

	@Override
	public void Draw(CArgumentGroup arg) 
	{
		// TODO Auto-generated method stub
		m_UpRect.Draw(arg);
		m_PressedRect.Draw(arg);		
	}

	@Override
	public boolean HandleTouch(int nPosX, int nPosY, int nTouchAction) 
	{
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public Type GetType() 
	{
		// TODO Auto-generated method stub
		return Type.BUTTON;
	}

	@Override
	public RectF CheckCollision(INyanObject inner) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void HandleCollision(INyanObject inner, RectF overlap) {
		// TODO Auto-generated method stub
		
	}
}
